/**
 * @class   wosh::SecurityFirewall
 * @brief   


 *
 ****************************************************************************
 * @version $Id: SecurityFirewall.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_SecurityFirewall_H__
 #define __WOSH_Core_SecurityFirewall_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/Containers.h>


namespace wosh {

class Bus;
class Message;
class WoshHost;
class NetworkChannel;
class SecurityManager;
class SecurityFirewall;


class SecurityPolicy : public ISerializable
 {
	WOSH_SERIALIZABLE_ABSTRACT(wosh::SecurityPolicy)
	WOSH_COMPARE_RTTI(wosh::SecurityPolicy)

	public:
		enum POLICY_ACTION {
			ACTION_UNKNOWN			= 0x00,
			ACTION_DENY				= 0x01,
			ACTION_ALLOW			= 0x02
		 };
		enum POLICY_TYPE {
			TYPE_UNKNOWN			= 0x00,
			TYPE_NETWORK_TRANSPORT	= 0x01,
			TYPE_USER_ACCESS		= 0x02,
			TYPE_MESSAGE			= 0x04
		 };

	public:
		SecurityPolicy();
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		SecurityPolicy( const SecurityPolicy& m ) {
			*this = m;
		 }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~SecurityPolicy() { }


	public:
		virtual WRESULT validate( const WoshHost* source, const WoshHost* target, const NetworkChannel* channel ) = 0;

		virtual WRESULT validate( const Message* message, const NetworkChannel* channel ) = 0;
		virtual WRESULT validate( const Message* message, const Bus* source, const Bus* target ) = 0;


/** @name Getters
 * @{
 ******************************************************************************/
	public:
		inline int64 getID() const							{ return this->policy_id; }

		virtual POLICY_TYPE getType() const = 0;
		inline POLICY_ACTION getActionSucceeded() const		{ return this->action_succeded; }
		inline POLICY_ACTION getActionFailed() const		{ return this->action_failed; }

		inline int64 getStatsFailed() const					{ return this->stats_failed; }


//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		inline void setActionSucceeded( POLICY_ACTION action )	{ this->action_succeded = action; }
		inline void setActionFailed( POLICY_ACTION action )		{ this->action_failed = action; }

		virtual void clear();
//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual SecurityPolicy& operator=(const SecurityPolicy& m);
		virtual bool operator==(const SecurityPolicy &other) const;
 		virtual bool operator!=(const SecurityPolicy &other) const { return !(*this == other);  }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const;
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	protected:
		friend class wosh::SecurityFirewall;

	protected:
		int64 policy_id;
		POLICY_ACTION action_succeded;
		POLICY_ACTION action_failed;

		int64 stats_failed;


}; // class def




class SecurityFirewall : public Object
 {
	WOSH_CLASSNAME(wosh::SecurityFirewall)
	WOSH_CLASS_ABSTRACT(false)

	public:
		SecurityFirewall();
		virtual ~SecurityFirewall() { }


	public:
		virtual WRESULT validate( const WoshHost* source, const WoshHost* target, const NetworkChannel* channel );

		virtual WRESULT validate( const Message* message, const NetworkChannel* channel );
		virtual WRESULT validate( const Message* message, const Bus* source, const Bus* target );



/** @name Getters
 * @{
 ******************************************************************************/
	public:


//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:


//@}
	protected:
		friend class wosh::SecurityManager;

	protected:
		ListT<SecurityPolicy*> trusted;
		ListT<SecurityPolicy*> denied;


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_SecurityFirewall_H__
